//
//  XFYAdaptiveLabel.m
//  SDKSample
//
//  Created by 小肥羊 on 15/7/3.
//  Copyright (c) 2015年 Zhu‘s Game House. All rights reserved.
//

#import "XFYAdaptiveLabel.h"

#pragma mark - 自适应Label
@implementation XFYAdaptiveLabel
#pragma mark - 初始化
- (id)init
{
    self = [super init];
    if (self) {
        self.labelMaxWidth = 100;
        [self setBackgroundColor:[UIColor lightGrayColor]];
        [self.layer setCornerRadius:5.0];
        [self addSubview:self.textLabel];
        [self addSubview:self.operationBtn];
    }
    return self;
}
- (id)initWithPoint:(CGPoint)porint
{
    self = [self init];
    if (self) {
        [self setFrame:(CGRect){porint, CGSizeMake(100, 100)}];
    }
    return self;
}

#pragma mark - 惰性初始化
- (UIButton *)operationBtn
{
    if (!_operationBtn) {
        _operationBtn = [[UIButton alloc] init];
        [_operationBtn setTitle:@"x" forState:UIControlStateNormal];
        [_operationBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_operationBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_operationBtn addTarget:self action:@selector(operation:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _operationBtn;
}

- (UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        [_textLabel setTextColor:[UIColor whiteColor]];
        [_textLabel setFont:[UIFont systemFontOfSize:14.0]];
    }
    return _textLabel;
}

#pragma mark - 显示
- (void)layoutSubviews
{
    [super layoutSubviews];
    float textWidth = self.textLabel.frame.size.width < self.labelMaxWidth - 30 - 12 ? self.textLabel.frame.size.width : self.labelMaxWidth - 30 - 12;
    [self.textLabel setFrame:(CGRect){CGPointMake(12, 4), CGSizeMake(textWidth, self.textLabel.frame.size.height)}];
    [self.operationBtn setFrame:CGRectMake(self.frame.size.width - 30, 0, 30, self.frame.size.height)];
    [self setFrame:(CGRect){self.frame.origin, CGSizeMake(textWidth + 30 + 12, self.textLabel.frame.size.height + 8)}];
}

#pragma mark - 方法
- (void)setText:(NSString *)text
{
    [self.textLabel setText:text];
    [self.textLabel sizeToFit];
    [self layoutIfNeeded];
    [self.operationBtn setFrame:CGRectMake(self.frame.size.width - 30, 0, 30, self.frame.size.height)];
}

#pragma mark - 点击操作
- (void)operation:(UIButton *)btn
{
    if (self.delegate) {
        [self.delegate operation:self];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


#pragma mark - 固定Label
@implementation XFYFixedLabel
#pragma mark - 初始化
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor lightGrayColor]];
        [self.layer setCornerRadius:5.0];
        [self addSubview:self.textLabel];
        [self addSubview:self.operationBtn];
    }
    return self;
}
#pragma mark - 惰性初始化
- (UIButton *)operationBtn
{
    if (!_operationBtn) {
        _operationBtn = [[UIButton alloc] init];
        [_operationBtn setTitle:@"x" forState:UIControlStateNormal];
        [_operationBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_operationBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_operationBtn addTarget:self action:@selector(operation:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _operationBtn;
}

- (UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        [_textLabel setTextColor:[UIColor whiteColor]];
        [_textLabel setFont:[UIFont systemFontOfSize:14.0]];
        [_textLabel setTextAlignment:NSTextAlignmentCenter];
    }
    return _textLabel;
}

#pragma mark - 显示
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.textLabel setFrame:(CGRect){CGPointMake(12, 0), CGSizeMake(self.frame.size.width - 42, self.frame.size.height)}];
    [self.operationBtn setFrame:CGRectMake(self.frame.size.width - 30, 0, 30, self.frame.size.height)];
}

#pragma mark - 点击操作
- (void)operation:(UIButton *)btn
{
    if (self.delegate) {
        [self.delegate fixedOperation:self];
    }
}
@end